package com.zzx.schoolfellow.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzx.schoolfellow.common.api.ApiResult;
import com.zzx.schoolfellow.jwt.JwtUtil;
import com.zzx.schoolfellow.mapper.SensitiveWordMapper;
import com.zzx.schoolfellow.model.dto.CommentDTO;
import com.zzx.schoolfellow.model.entity.BmsComment;
import com.zzx.schoolfellow.model.entity.SensitiveWord;
import com.zzx.schoolfellow.model.entity.UmsUser;
import com.zzx.schoolfellow.model.vo.CommentVO;
import com.zzx.schoolfellow.service.IBmsCommentService;
import com.zzx.schoolfellow.service.IUmsUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


/**
 * @program: schoolfeoolw_backend
 * @description:
 * @author: zzx
 * @create: 2023-03-14 21:56
 **/
@RestController
@RequestMapping("/comment")
public class BmsCommentController extends BaseController {

    @Resource
    private IBmsCommentService bmsCommentService;
    @Resource
    private IUmsUserService umsUserService;

    @Resource
    private SensitiveWordMapper sensitiveWordMapper;

    @GetMapping("/get_comments")
    public ApiResult<List<CommentVO>> getCommentsByTopicID(@RequestParam(value = "topicid", defaultValue = "1") String topicid) {
        List<CommentVO> lstBmsComment = bmsCommentService.getCommentsByTopicID(topicid);
        return ApiResult.success(lstBmsComment);
    }

    @PostMapping("/add_comment")
    public ApiResult<BmsComment> add_comment(@RequestHeader(value = JwtUtil.USER_NAME) String userName,
                                             @RequestBody CommentDTO dto) {
        UmsUser user = umsUserService.getUserByUsername(userName);
        BmsComment comment = bmsCommentService.create(dto, user);
        return ApiResult.success(comment);
    }

    //    @GetMapping("/check")
//    public boolean checkShieldWords(@RequestParam("commentText") String content) {
//        QueryWrapper<SensitiveWord> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("word");
//        queryWrapper.orderByAsc("id");
//        List<SensitiveWord> shieldWords = sensitiveWordMapper.selectList(queryWrapper);
//        String regex = shieldWords.stream().map(word -> "\\b" + word.getWord() + "\\b").collect(Collectors.joining("|"));
//        return !Pattern.compile(regex, Pattern.CASE_INSENSITIVE).matcher(content).find();
//    }
// 检查并替换评论中的敏感词汇
    @GetMapping("/check")
    public String checkAndReplaceSensitiveWords(@RequestParam("commentText") String content) {
        List<String> words = splitContent(content);
        QueryWrapper<SensitiveWord> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("word");
        queryWrapper.orderByAsc("id");
        List<SensitiveWord> sensitiveWords = sensitiveWordMapper.selectList(queryWrapper);
        StringBuilder sb = new StringBuilder(content);
        for (SensitiveWord sensitiveWord : sensitiveWords) {
            String word = sensitiveWord.getWord();
            if (content.contains(word)) {
                int index = sb.indexOf(word);
                while (index != -1) {
                    sb.replace(index, index + word.length(), StringUtils.repeat("*", word.length()));
                    index = sb.indexOf(word, index + 1);
                }
            }
        }
        return sb.toString();
    }

    // 将文本分字切割
    private List<String> splitContent(String content) {
        List<String> words = new ArrayList<>();
        for (int i = 0; i < content.length(); i++) {
            words.add(String.valueOf(content.charAt(i)));
        }
        return words;
    }
}
